function IncludeJavaScript(jsFile)
{
  document.write('<script type="text/javascript" src="'
    + jsFile + '"></scr' + 'ipt>'); 
}


IncludeJavaScript("http://yui.yahooapis.com/2.7.0/build/yahoo/yahoo-min.js");
IncludeJavaScript("http://yui.yahooapis.com/2.7.0/build/event/event-min.js");
IncludeJavaScript("http://yui.yahooapis.com/2.7.0/build/connection/connection-min.js");



function Point(x, y, epsg) {
    this.x = x;
    this.y = y;
    this.epsg = epsg;
    
    this.getX = function() {
        return this.x;
    };
    
    this.getY = function() {
        return this.y;
    };
    
    this.getEPSG = function() {
        return this.epsg;
    };
}

function Observation(id, location, value)
{
    this.id = id;
    this.location = location;
    this.value = value;
    
    this.getID = function() {
        return this.id;
    };
    
    this.getLocation = function() {
        return this.location;
    };
    
    this.getResult = function() {
        return this.value;
    };
}

function ObservationCollection(observations) {
    this.obs = observations;
    
    this.getObservation = function(index) {
        return this.obs[index];
    };
    
    this.getObservations = function() {
        return this.obs;
    };
    
    this.getLength = function() {
        return this.obs.length;  
    };
}

function SOS(URL)
{
    this.URL = URL;
    this.callback = null;
    var that = this;
      
    this.responseFailure = function(o){
        // Access the response object's properties in the
        // same manner as listed in responseSuccess( ).
        // Please see the Failure Case section and
        // Communication Error sub-section for more details on the
        // response object's properties.
        alert('Failure' + o.statusText);
    };
    
    this.getObservationsSuccess = function(o) {
        var xml = o.responseXML;
        var obsArray = new Array();
        var obs = xml.getElementsByTagName('om:Measurement');
        for(var i = 0; i < obs.length; i++)
        {
            var o = obs[i];
            var id = o.attributes.getNamedItem('gml:id').value;
            var result = o.getElementsByTagName('om:result')[0].childNodes[0].nodeValue;
            var loc = o.getElementsByTagName('om:featureOfInterest')[0].getElementsByTagName('sa:SamplingPoint')[0].getElementsByTagName('sa:position')[0].getElementsByTagName('gml:Point')[0].getElementsByTagName('gml:pos')[0].childNodes[0].nodeValue;
            var x = loc.split(" ")[0];
            var y = loc.split(" ")[1];
            var point = new Point(x, y, 4326);
            var ob = new Observation(id, point, result);
            obsArray[i] = ob;            
        }
        that.callback.call(this, new ObservationCollection(obsArray));
    };
      
    this.getObservationsCallback =
    {
       success:this.getObservationsSuccess,
       failure:this.responseFailure
    }; 
    
    this.getURL = function() { return this.URL };
    
    this.getObservations = function(callback, offering, phenomenon, startTime, endTime) 
    {
        that.callback = callback;
        if(offering == null)
        {
            // We need an offering to continue;
            alert('please specify an offering');
            return;
        }
        if(phenomenon == null)
        {
            alert('please specify a phenomenon');
            return;
        }
        
        var xml = "request=GetObservation&url="+this.URL+"&offering="+offering+"&phenomenon="+phenomenon+"&epsg=4326&resultModel=om:Measurement";
        if(endTime != null)
        {
            xml += "&endTime="+endTime;
        }
        if(startTime != null)
        {
            xml += "&startTime="+startTime;
        }
        var transaction = YAHOO.util.Connect.asyncRequest('POST', 'sos.php', this.getObservationsCallback, xml);
    };
        
}


